package com.stats.ultimate;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Game extends Activity {
	// Declare variables to hold SQLiteDatabase and database helper variables
	SQLiteDatabase db;
	DatabaseHelper db_helper;
	
	// Declare variable to hold team id
	int id;
	
	// Declare variable to hold number of players on this team
	int num_players;
	
	// Declare 4 variables to hold teams stats
	int team_goals;
	int team_assists;
	int team_d;
	int team_to;
	
	// Declare 4 TextViews to display team's stats
	TextView team_goals_view;
	TextView team_assists_view;
	TextView team_d_view;
	TextView team_to_view;
	
	// Declare arrays of ints to hold stats for each player
	ArrayList<Integer> goals = new ArrayList<Integer>();
	ArrayList<Integer> assists = new ArrayList<Integer>();
	ArrayList<Integer> d = new ArrayList<Integer>();
	ArrayList<Integer> to = new ArrayList<Integer>();
	
	// Declare array of TextViews to hold displays for each players name
	ArrayList<TextView> names = new ArrayList<TextView>();
	
	// Declare arrays of buttons to hold plus and minus buttons for each player for each stat
	ArrayList<Button> goals_plus = new ArrayList<Button>();
	ArrayList<Button> assists_plus = new ArrayList<Button>();
	ArrayList<Button> d_plus = new ArrayList<Button> ();
	ArrayList<Button> to_plus = new ArrayList<Button>();
	ArrayList<Button> goals_minus = new ArrayList<Button>();
	ArrayList<Button> assists_minus = new ArrayList<Button>();
	ArrayList<Button> d_minus = new ArrayList<Button>();
	ArrayList<Button> to_minus = new ArrayList<Button>();
	
	// Declare arrays of TextViews to hold displays of stats for each player
	ArrayList<TextView> goals_views = new ArrayList<TextView>();
	ArrayList<TextView> assists_views = new ArrayList<TextView>();
	ArrayList<TextView> d_views = new ArrayList<TextView>();
	ArrayList<TextView> to_views = new ArrayList<TextView>();
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);
        
        // Instantiate new db_helper 
        db_helper = new DatabaseHelper(this);
    	
        // Check if savedInstanceState is non-null
        if(savedInstanceState != null){
        	// If so get id from savedInstanceState bundle
        	id = savedInstanceState.getInt("id");
        	
            // Get number of players from savedInstanceState bundle
        	num_players = savedInstanceState.getInt("num_players");

        	// Get goals array from savedInstanceState bundle
        	goals = savedInstanceState.getIntegerArrayList("goals");
        	
        	// Get assists array from savedInstanceState bundle
        	assists = savedInstanceState.getIntegerArrayList("assists");
        	
        	// Get d array from savedInstanceState bundle
        	d = savedInstanceState.getIntegerArrayList("d");
        	
        	// Get to array from savedInstanceState bundle
        	to = savedInstanceState.getIntegerArrayList("to");
        	
        	// Get team_goals from savedInstanceState bundle
        	team_goals = savedInstanceState.getInt("team_goals");
        	
        	// Get team_assists from savedInstanceState bundle
        	team_assists = savedInstanceState.getInt("team_assists");
        	
        	// Get team_d from savedInstanceState bundle
        	team_d = savedInstanceState.getInt("team_d");
        	
        	// Get team_to from savedInstanceState bundle
        	team_to = savedInstanceState.getInt("team_to");
        }
        
        // Else this is a new activity
        else{
        	// If so get id from previous activity
        	id = this.getIntent().getIntExtra("id", 1);
        	
            // Open database for reading
            db = db_helper.getWritableDatabase();
            
        	// Get number of players for this team
        	num_players = db_helper.getNumberPlayers(db, id);
        	
        	// Close db
        	db.close();
        	
        	// Iterate once for each player
        	for(int i = 0; i < num_players; i++){
        		// Initialize every stat for this player to 0
        		goals.add(0);
        		assists.add(0);
        		d.add(0);
        		to.add(0);
        	}
        	
        	// Initialize team stats to 0
        	team_goals = 0;
        	team_assists = 0;
        	team_d = 0;
        	team_to = 0;
        }

        // Instantiate title textView
        TextView title = (TextView) findViewById(R.id.title);
        
        // Open the database for reading
        db = db_helper.getWritableDatabase();
        
        // Read this teams name from the database
        String team_name = db_helper.getTeamName(db, id);
        
        // Close the database
        db.close();
        
        // Set the title of this page to be the team name
        title.setText(team_name);
        
        // Instantiate save button
        Button save = (Button) findViewById(R.id.save);
        
        // Initialize text views to hold team stats
        team_goals_view = (TextView) findViewById(R.id.teamGoals);
        team_assists_view = (TextView) findViewById(R.id.teamAssists);
        team_d_view = (TextView) findViewById(R.id.teamD);
        team_to_view = (TextView) findViewById(R.id.teamTo);
        
        // Format strings that hold team stat values and display them in text views
        team_goals_view.setText(String.format("%d", team_goals));
        team_assists_view.setText(String.format("%d", team_assists));
        team_d_view.setText(String.format("%d", team_d));
        team_to_view.setText(String.format("%d", team_to));
        
        // Initialize relative layout that is inside a scrollview defined in layout
        RelativeLayout l1 = (RelativeLayout) findViewById(R.id.relativeLayout1);
        
        // Declare textview to hold first players name
        TextView first_player = new TextView(this);
        
        // Open the database for reading
        db = db_helper.getWritableDatabase();
        
        // Get first players name and make it the text of first_player
        first_player.setText(db_helper.getPlayerName(db, id, 1));
        
        // Close the database
        db.close();

		// Define the layout parameters to be used for first_player
		RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

		// Set the margins of first player to be 10 on the left, and 40 on the top and bottom
		params1.setMargins(10, 40, 0, 40);
		
		// Set the parameters of first_player
		first_player.setLayoutParams(params1);
		
		// Set the id of first_player to be 101
		first_player.setId(101);
		
		// Add first_player to layout
		l1.addView(first_player);
		
		// Add first_player to names array
		names.add(first_player);
				
		// Declare textview to hold first players to view
		TextView first_to_view = new TextView(this);
		
		// Get first players to's, format it, and make it the text of first_to_view
        first_to_view.setText(String.format("%d", to.get(0)));

		// Define the layout parameters to be used for first_to_view
		RelativeLayout.LayoutParams params5 = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		

		// Set first_to_view to be right aligned in parent
		params5.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		
		// Set right margin of first_to_view to be 30
		params5.rightMargin = 30;
		
		// Set first_to_view to be top aligned with first_player
		params5.addRule(RelativeLayout.ALIGN_TOP, 101);
		
		// Set the parameters of first_to_view
		first_to_view.setLayoutParams(params5);
		
		// Set the id of first_to_view to be 501
		first_to_view.setId(501);
		
		// Add first_to_view to l1
		l1.addView(first_to_view);
		
		// Add first_to_view to to_views array
		to_views.add(first_to_view);
		
		// Declare textview to hold first players d view
		TextView first_d_view = new TextView(this);
		
		// Get first players d's, format it, and make it the text of first_d_view
        first_d_view.setText(String.format("%d", d.get(0)));

		// Define the layout parameters to be used for first_d_view
		RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		

		// Set first_d_view to be to the left of first_to_view
		params4.addRule(RelativeLayout.LEFT_OF, 501);
		
		// Set right margin of first_d_view to be 27
		params4.rightMargin = 27;
		
		// Set first_d_view to be top aligned with first_player
		params4.addRule(RelativeLayout.ALIGN_TOP, 101);
		
		// Set the parameters of first_d_view
		first_d_view.setLayoutParams(params4);
		
		// Set the id of first_d_view to be 401
		first_d_view.setId(401);
		
		// Add first_d_view to l1
		l1.addView(first_d_view);
		
		// Add first_d_view to d_views array
		d_views.add(first_d_view);

		// Declare textview to hold first players assists view
		TextView first_assists_view = new TextView(this);
		
		// Get first players assists, format it, and make it the text of first_assists_view
        first_assists_view.setText(String.format("%d", assists.get(0)));

		// Define the layout parameters to be used for first_assists_view
		RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		

		// Set first_assists_view to be to the left of first_d_view
		params3.addRule(RelativeLayout.LEFT_OF, 401);
		
		// Set right margin of first_assists_view to be 55
		params3.rightMargin = 55;
		
		// Set first_assists_view to be top aligned with first_player
		params3.addRule(RelativeLayout.ALIGN_TOP, 101);
		
		// Set the parameters of first_assists_view
		first_assists_view.setLayoutParams(params3);
		
		// Set the id of first_assists_view to be 301
		first_assists_view.setId(301);
		
		// Add first_assists_view to l1
		l1.addView(first_assists_view);
		
		// Add first_assists_view to assists_views array
		assists_views.add(first_assists_view);

		// Declare textview to hold first players goals view
		TextView first_goals_view = new TextView(this);
		
		// Get first players goals, format it, and make it the text of first_goals_view
        first_goals_view.setText(String.format("%d", goals.get(0)));

		// Define the layout parameters to be used for first_goals_view
		RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		
        
		// Set first_goals_view to be to the left of first_assists_view
		params2.addRule(RelativeLayout.LEFT_OF, 301);
		
		// Set right margin of first_goals_view to be 48
		params2.rightMargin = 48;
		
		// Set first_goals_view to be top aligned with first_player
		params2.addRule(RelativeLayout.ALIGN_TOP, 101);
		
		// Set the parameters of first_goals_view
		first_goals_view.setLayoutParams(params2);
		
		// Set the id of first_goals_view to be 201
		first_goals_view.setId(201);
		
		// Add first_goals_view to l1
		l1.addView(first_goals_view);
		
		// Add first_goals_view to goals_views array
		goals_views.add(first_goals_view);

		// Create plus button for first players goals
		Button first_goals_plus = new Button(this);

		// Set the text of first_goals_plus to be +
		first_goals_plus.setText("+");
		
		// Define the layout parameters to be used for first_goals_plus
		RelativeLayout.LayoutParams params6 = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		
 
		// Set the height of first_goals_plus to be 30
		params6.height = 30;
		
		// Set first_goals_plus to be above first_goals_view
		params6.addRule(RelativeLayout.ABOVE, first_goals_view.getId());
		
		// Set first_goals_plus to be right aligned with first_goals_view
		params6.addRule(RelativeLayout.ALIGN_RIGHT, first_goals_view.getId());
		
		// Set parameters of first_goals_plus
		first_goals_plus.setLayoutParams(params6);
		
		// Set id of first_goals_plus to be 601
		first_goals_plus.setId(601);
		
		// Add first_goals_plus to the layout
		l1.addView(first_goals_plus);
		
		// Add first_goals_plus to goals_plus array
		goals_plus.add(first_goals_plus);
		
		// Create minus button for first players goals
		Button first_goals_minus = new Button(this);

		// Set the text of first_goals_minus to be -
		first_goals_minus.setText("-");
		
		// Define the layout parameters to be used for first_goals_minus
		RelativeLayout.LayoutParams params7 = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		
 
		// Set first_goals_minus to be below first_goals_view
		params7.addRule(RelativeLayout.BELOW, first_goals_view.getId());
		
		// Set height of first_goals_minus to be 30
		params7.height = 30;
		
		// Set first_goals_minus to be right and left aligned with first_goals_plus
		params7.addRule(RelativeLayout.ALIGN_RIGHT, first_goals_plus.getId());
		params7.addRule(RelativeLayout.ALIGN_LEFT, first_goals_plus.getId());
		
		// Set parameters of first_goals_minus
		first_goals_minus.setLayoutParams(params7);
		
		// Set id of first_goals_minus to be 701
		first_goals_minus.setId(701);
		
		// Add first_goals_minus to the layout
		l1.addView(first_goals_minus);
		
		// Add first_goals_minus to goals_minus array
		goals_minus.add(first_goals_minus);

		// Create plus button for first players assists
		Button first_assists_plus = new Button(this);

		// Set the text of first_assists_plus to be +
		first_assists_plus.setText("+");
		
		// Define the layout parameters to be used for first_assists_plus
		RelativeLayout.LayoutParams params8 = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		
 
		// Set first_assists_plus to be above first_assists_view
		params8.addRule(RelativeLayout.ABOVE, first_assists_view.getId());
		
		// Set the height of first_assists_plus to be 30
		params8.height = 30;
		
		// Set first_assists_plus to be right aligned with first_assists_view
		params8.addRule(RelativeLayout.ALIGN_RIGHT, first_assists_view.getId());
		
		// Set parameters of first_assists_plus
		first_assists_plus.setLayoutParams(params8);
		
		// Set id of first_assists_plus to be 801
		first_assists_plus.setId(801);
		
		// Add first_assists_plus to the layout
		l1.addView(first_assists_plus);
		
		// Add first_assists_plus to assists_plus array
		assists_plus.add(first_assists_plus);
		
		// Create minus button for first players assists
		Button first_assists_minus = new Button(this);

		// Set the text of first_assists_minus to be -
		first_assists_minus.setText("-");
		
		// Define the layout parameters to be used for first_assists_minus
		RelativeLayout.LayoutParams params9 = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		
 
		// Set first_assists_minus to be below first_assists_view
		params9.addRule(RelativeLayout.BELOW, first_assists_view.getId());

		// Set the height of first_assists_minus to be 30
		params9.height = 30;
		
		// Set first_assists_minus to be right and left aligned with first_assists_plus
		params9.addRule(RelativeLayout.ALIGN_RIGHT, first_assists_plus.getId());
		params9.addRule(RelativeLayout.ALIGN_LEFT, first_assists_plus.getId());
		
		// Set parameters of first_assists_minus
		first_assists_minus.setLayoutParams(params9);
		
		// Set id of first_assists_minus to be 901
		first_assists_minus.setId(901);
		
		// Add first_assists_minus to the layout
		l1.addView(first_assists_minus);
		
		// Add first_assists_minus to assists_minus array
		assists_minus.add(first_assists_minus);

		// Create plus button for first players d
		Button first_d_plus = new Button(this);

		// Set the text of first_d_plus to be +
		first_d_plus.setText("+");
		
		// Define the layout parameters to be used for first_d_plus
		RelativeLayout.LayoutParams params10 = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		
 
		// Set first_d_plus to be above first_d_view
		params10.addRule(RelativeLayout.ABOVE, first_d_view.getId());
		
		// Set the height of first_d_plus to be 30
		params10.height = 30;
		
		// Set first_d_plus to be right aligned with first_d_view
		params10.addRule(RelativeLayout.ALIGN_RIGHT, first_d_view.getId());
		
		// Set parameters of first_d_plus
		first_d_plus.setLayoutParams(params10);
		
		// Set id of first_d_plus to be 1001
		first_d_plus.setId(1001);
		
		// Add first_d_plus to the layout
		l1.addView(first_d_plus);
		
		// Add first_d_plus to d_plus array
		d_plus.add(first_d_plus);
		
		// Create minus button for first players d
		Button first_d_minus = new Button(this);

		// Set the text of first_d_minus to be -
		first_d_minus.setText("-");
		
		// Define the layout parameters to be used for first_d_minus
		RelativeLayout.LayoutParams params11 = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		
 
		// Set first_d_minus to be below first_d_view
		params11.addRule(RelativeLayout.BELOW, first_d_view.getId());
		
		// Set the height of first_d_minus to be 30
		params11.height = 30;
		
		// Set first_d_minus to be right and left aligned with first_d_plus
		params11.addRule(RelativeLayout.ALIGN_RIGHT, first_d_plus.getId());
		params11.addRule(RelativeLayout.ALIGN_LEFT, first_d_plus.getId());
		
		// Set parameters of first_d_minus
		first_d_minus.setLayoutParams(params11);
		
		// Set id of first_d_minus to be 1101
		first_d_minus.setId(1101);
		
		// Add first_d_minus to the layout
		l1.addView(first_d_minus);
		
		// Add first_d_minus to d_minus array
		d_minus.add(first_d_minus);

		// Create plus button for first players to
		Button first_to_plus = new Button(this);

		// Set the text of first_to_plus to be +
		first_to_plus.setText("+");
		
		// Define the layout parameters to be used for first_goals_plus
		RelativeLayout.LayoutParams params12 = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		
 
		// Set first_to_plus to be above first_to_view
		params12.addRule(RelativeLayout.ABOVE, first_to_view.getId());
		
		// Set the height of first_to_plus to be 30
		params12.height = 30;
		
		// Set first_to_plus to be right aligned with first_to_view
		params12.addRule(RelativeLayout.ALIGN_RIGHT, first_to_view.getId());
		
		// Set parameters of first_to_plus
		first_to_plus.setLayoutParams(params12);
		
		// Set id of first_to_plus to be 1201
		first_to_plus.setId(1201);
		
		// Add first_to_plus to the layout
		l1.addView(first_to_plus);
		
		// Add first_to_plus to to_plus array
		to_plus.add(first_to_plus);
		
		// Create minus button for first players to
		Button first_to_minus = new Button(this);

		// Set the text of first_to_minus to be -
		first_to_minus.setText("-");
		
		// Define the layout parameters to be used for first_to_minus
		RelativeLayout.LayoutParams params13 = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		
 
		// Set first_to_minus to be below first_to_view
		params13.addRule(RelativeLayout.BELOW, first_to_view.getId());
		
		// Set the height of first_to_minus to be 30
		params13.height = 30;
		
		// Set first_to_minus to be right and left aligned with first_to_plus
		params13.addRule(RelativeLayout.ALIGN_RIGHT, first_to_plus.getId());
		params13.addRule(RelativeLayout.ALIGN_LEFT, first_to_plus.getId());
		
		// Set parameters of first_to_minus
		first_to_minus.setLayoutParams(params13);
		
		// Set id of first_to_minus to be 1301
		first_to_minus.setId(1301);
		
		// Add first_to_minus to the layout
		l1.addView(first_to_minus);
		
		// Add first_to_minus to to_minus array
		to_minus.add(first_to_minus);
		
		// Set onClickListener for first_goals_plus
		first_goals_plus.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Increment the goals for first player
				goals.set(0, goals.get(0)+1);
				
				// Increment team_goals
				team_goals++;
				
				// Change the text in the corresponding TextView
				goals_views.get(0).setText(String.format("%d", goals.get(0)));
				
				// Team stats views
				updateTeamViews();
			}
		});
		
		// Set onClickListener for first_goals_minus
		first_goals_minus.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Check if goals for first player are positive
				if(goals.get(0) > 0){
					// If so decrement goals for first player
					goals.set(0, goals.get(0)-1);
					
					// Decrement team_goals
					team_goals--;
				}
				
				// Change the text in the corresponding TextView
				goals_views.get(0).setText(String.format("%d", goals.get(0)));
			
				// Team stats views
				updateTeamViews();
			}
		});
		
		// Set onClickListener for first_assists_plus
		first_assists_plus.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Increment the assists for first player
				assists.set(0, assists.get(0)+1);
				
				// Increment team_assists
				team_assists++;
				
				// Change the text in the corresponding TextView
				assists_views.get(0).setText(String.format("%d", assists.get(0)));

				// Team stats views
				updateTeamViews();
			}
		});
		
		// Set onClickListener for first_assists_minus
		first_assists_minus.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Check if assists for first player are positive
				if(assists.get(0)> 0){
					// If so decrement assists for first player
					assists.set(0, assists.get(0)-1);
					
					// Decrement team_assists
					team_assists--;
				}
				
				// Change the text in the corresponding TextView
				assists_views.get(0).setText(String.format("%d", assists.get(0)));

				// Team stats views
				updateTeamViews();
			}
		});
		
		// Set onClickListener for first_d_plus
		first_d_plus.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Increment the d for first player
				d.set(0, d.get(0)+1);
				
				// Increment team_d
				team_d++;
				
				// Change the text in the corresponding TextView
				d_views.get(0).setText(String.format("%d", d.get(0)));

				// Team stats views
				updateTeamViews();
			}
		});
		
		// Set onClickListener for first_d_minus
		first_d_minus.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Check if d for first player are positive
				if(d.get(0) > 0){
					// If so decrement d for first player
					d.set(0, d.get(0)-1);
					
					// Decrement team_d
					team_d--;
				}
				
				// Change corresponding TextView
				d_views.get(0).setText(String.format("%d", d.get(0)));

				// Team stats views
				updateTeamViews();
			}
		});
		
		// Set onClickListener for first_to_plus
		first_to_plus.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Increment the to for first player
				to.set(0, to.get(0)+1);
				
				// Increment team_to
				team_to++;
				// Change corresponding TextView
				to_views.get(0).setText(String.format("%d", to.get(0)));

				// Team stats views
				updateTeamViews();
			}
		});
		
		// Set onClickListener for first_to_minus
		first_to_minus.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Check if to for first player are positive
				if(to.get(0) > 0){
					// If so decrement to for first player
					to.set(0, to.get(0)-1);
					
					// Decrement team_to
					team_to--;
				}
				
				// Change corresponding TextView
				to_views.get(0).setText(String.format("%d", to.get(0)));

				// Team stats views
				updateTeamViews();
			}
		});
		
		// Iterate through remaining players
		for(int i = 2; i <= num_players; i++){
			// Declare textview to hold this players name
	        TextView temp_player = new TextView(this);
	        
	        // Open the database for reading
	        db = db_helper.getWritableDatabase();
	        
	        // Get this players name and make it the text of first_player
	        temp_player.setText(db_helper.getPlayerName(db, id, i));
	        
	        // Close the database
	        db.close();

			// Define the layout parameters to be used for temp_player
			RelativeLayout.LayoutParams temp_params1 = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

			// Set temp_player to be below the last player
			temp_params1.addRule(RelativeLayout.BELOW, 100+i-1);

			// Set temp_player to be left aligned with the last player
			temp_params1.addRule(RelativeLayout.ALIGN_LEFT, 100+i-1);
			
			// Set the margins of temp_player to be 40 on the top and bottom
			temp_params1.setMargins(0, 40, 0, 40);
			
			// Set the parameters of temp_player
			temp_player.setLayoutParams(temp_params1);
			
			// Set the id of temp_player to be 100+i
			temp_player.setId(100+i);
			
			// Add temp_player to layout
			l1.addView(temp_player);
			
			// Add temp_player to names array
			names.add(temp_player);
					
			// Declare textview to hold this players to view
			TextView temp_to_view = new TextView(this);
			
			// Get this players to's, format it, and make it the text of temp_to_view
	        temp_to_view.setText(String.format("%d", to.get(i-1)));

			// Define the layout parameters to be used for first_to_view
			RelativeLayout.LayoutParams temp_params5 = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		

			// Set temp_to_view to be right aligned with the view above it
			temp_params5.addRule(RelativeLayout.ALIGN_RIGHT, 500+i-1);
			
			// Set temp_to_view to be top aligned with temp_player
			temp_params5.addRule(RelativeLayout.ALIGN_TOP, temp_player.getId());
			
			// Set the parameters of temp_to_view
			temp_to_view.setLayoutParams(temp_params5);
			
			// Set the id of temp_to_view to be 500+i
			temp_to_view.setId(500+i);
			
			// Add temp_to_view to l1
			l1.addView(temp_to_view);
			
			// Add temp_to_view to to_views array
			to_views.add(temp_to_view);
			
			// Declare textview to hold this players d view
			TextView temp_d_view = new TextView(this);
			
			// Get this players d's, format it, and make it the text of temp_d_view
	        temp_d_view.setText(String.format("%d", d.get(i-1)));

			// Define the layout parameters to be used for temp_d_view
			RelativeLayout.LayoutParams temp_params4 = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		

			// Set temp_d_view to be right aligned with the view above it
			temp_params4.addRule(RelativeLayout.ALIGN_RIGHT, 400+i-1);
			
			// Set temp_d_view to be top aligned with temp_player
			temp_params4.addRule(RelativeLayout.ALIGN_TOP, temp_player.getId());
			
			// Set the parameters of temp_d_view
			temp_d_view.setLayoutParams(temp_params4);
			
			// Set the id of temp_d_view to be 400+i
			temp_d_view.setId(400+i);
			
			// Add temp_d_view to l1
			l1.addView(temp_d_view);
			
			// Add temp_d_view to d_views array
			d_views.add(temp_d_view);

			// Declare textview to hold this players assists view
			TextView temp_assists_view = new TextView(this);
			
			// Get this players assists, format it, and make it the text of temp_assists_view
	        temp_assists_view.setText(String.format("%d", assists.get(i-1)));

			// Define the layout parameters to be used for temp_assists_view
			RelativeLayout.LayoutParams temp_params3 = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		

			// Set temp_assists_view to be right aligned with the view above it
			temp_params3.addRule(RelativeLayout.ALIGN_RIGHT, 300+i-1);
			
			// Set temp_assists_view to be top aligned with temp_player
			temp_params3.addRule(RelativeLayout.ALIGN_TOP, temp_player.getId());
			
			// Set the parameters of temp_assists_view
			temp_assists_view.setLayoutParams(temp_params3);
			
			// Set the id of temp_assists_view to be 300+i
			temp_assists_view.setId(300+i);
			
			// Add temp_assists_view to l1
			l1.addView(temp_assists_view);
			
			// Add temp_assists_view to assists_views array
			assists_views.add(temp_assists_view);

			// Declare textview to hold this players goals view
			TextView temp_goals_view = new TextView(this);
			
			// Get this players goals, format it, and make it the text of temp_goals_view
	        temp_goals_view.setText(String.format("%d", goals.get(i-1)));

			// Define the layout parameters to be used for temp_goals_view
			RelativeLayout.LayoutParams temp_params2 = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		
	        
			// Set temp_goals_view to be right aligned with the view above it
			temp_params2.addRule(RelativeLayout.ALIGN_RIGHT, 200+i-1);
			
			// Set temp_goals_view to be top aligned with temp_player
			temp_params2.addRule(RelativeLayout.ALIGN_TOP, temp_player.getId());
			
			// Set the parameters of temp_goals_view
			temp_goals_view.setLayoutParams(temp_params2);
			
			// Set the id of temp_goals_view to be 200+i
			temp_goals_view.setId(200+i);
			
			// Add temp_goals_view to l1
			l1.addView(temp_goals_view);
			
			// Add temp_goals_view to goals_views array
			goals_views.add(temp_goals_view);

			// Create plus button for this players goals
			Button temp_goals_plus = new Button(this);

			// Set the text of temp_goals_plus to be +
			temp_goals_plus.setText("+");
			
			// Define the layout parameters to be used for temp_goals_plus
			RelativeLayout.LayoutParams temp_params6 = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		
	 
			// Set the height of temp_goals_plus to be 30
			temp_params6.height = 30;
			
			// Set temp_goals_plus to be above temp_goals_view
			temp_params6.addRule(RelativeLayout.ABOVE, temp_goals_view.getId());
			
			// Set temp_goals_plus to be right aligned with temp_goals_view
			temp_params6.addRule(RelativeLayout.ALIGN_RIGHT, temp_goals_view.getId());
			
			// Set parameters of temp_goals_plus
			temp_goals_plus.setLayoutParams(temp_params6);
			
			// Set id of temp_goals_plus to be 600+i
			temp_goals_plus.setId(600+i);
			
			// Add temp_goals_plus to the layout
			l1.addView(temp_goals_plus);
			
			// Add temp_goals_plus to goals_plus array
			goals_plus.add(temp_goals_plus);
			
			// Create minus button for this players goals
			Button temp_goals_minus = new Button(this);

			// Set the text of temp_goals_minus to be -
			temp_goals_minus.setText("-");
			
			// Define the layout parameters to be used for temp_goals_minus
			RelativeLayout.LayoutParams temp_params7 = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		
	 
			// Set temp_goals_minus to be below temp_goals_view
			temp_params7.addRule(RelativeLayout.BELOW, temp_goals_view.getId());
			
			// Set height of temp_goals_minus to be 30
			temp_params7.height = 30;
			
			// Set temp_goals_minus to be right and left aligned with temp_goals_plus
			temp_params7.addRule(RelativeLayout.ALIGN_RIGHT, temp_goals_plus.getId());
			temp_params7.addRule(RelativeLayout.ALIGN_LEFT, temp_goals_plus.getId());
			
			// Set parameters of temp_goals_minus
			temp_goals_minus.setLayoutParams(temp_params7);
			
			// Set id of temp_goals_minus to be 700+i
			temp_goals_minus.setId(700+i);
			
			// Add temp_goals_minus to the layout
			l1.addView(temp_goals_minus);
			
			// Add temp_goals_minus to goals_minus array
			goals_minus.add(temp_goals_minus);

			// Create plus button for this players assists
			Button temp_assists_plus = new Button(this);

			// Set the text of temp_assists_plus to be +
			temp_assists_plus.setText("+");
			
			// Define the layout parameters to be used for temp_assists_plus
			RelativeLayout.LayoutParams temp_params8 = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		
	 
			// Set temp_assists_plus to be above temp_assists_view
			temp_params8.addRule(RelativeLayout.ABOVE, temp_assists_view.getId());
			
			// Set the height of temp_assists_plus to be 30
			temp_params8.height = 30;
			
			// Set temp_assists_plus to be right aligned with temp_assists_view
			temp_params8.addRule(RelativeLayout.ALIGN_RIGHT, temp_assists_view.getId());
			
			// Set parameters of temp_assists_plus
			temp_assists_plus.setLayoutParams(temp_params8);
			
			// Set id of temp_assists_plus to be 800+i
			temp_assists_plus.setId(800+i);
			
			// Add temp_assists_plus to the layout
			l1.addView(temp_assists_plus);
			
			// Add temp_assists_plus to assists_plus array
			assists_plus.add(temp_assists_plus);
			
			// Create minus button for this players assists
			Button temp_assists_minus = new Button(this);

			// Set the text of temp_assists_minus to be -
			temp_assists_minus.setText("-");
			
			// Define the layout parameters to be used for temp_assists_minus
			RelativeLayout.LayoutParams temp_params9 = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		
	 
			// Set temp_assists_minus to be below temp_assists_view
			temp_params9.addRule(RelativeLayout.BELOW, temp_assists_view.getId());

			// Set the height of temp_assists_minus to be 30
			temp_params9.height = 30;
			
			// Set temp_assists_minus to be right and left aligned with temp_assists_plus
			temp_params9.addRule(RelativeLayout.ALIGN_RIGHT, temp_assists_plus.getId());
			temp_params9.addRule(RelativeLayout.ALIGN_LEFT, temp_assists_plus.getId());
			
			// Set parameters of temp_assists_minus
			temp_assists_minus.setLayoutParams(temp_params9);
			
			// Set id of temp_assists_minus to be 900+i
			temp_assists_minus.setId(900+i);
			
			// Add temp_assists_minus to the layout
			l1.addView(temp_assists_minus);
			
			// Add temp_assists_minus to assists_minus array
			assists_minus.add(temp_assists_minus);

			// Create plus button for this players d
			Button temp_d_plus = new Button(this);

			// Set the text of temp_d_plus to be +
			temp_d_plus.setText("+");
			
			// Define the layout parameters to be used for temp_d_plus
			RelativeLayout.LayoutParams temp_params10 = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		
	 
			// Set temp_d_plus to be above temp_d_view
			temp_params10.addRule(RelativeLayout.ABOVE, temp_d_view.getId());
			
			// Set the height of temp_d_plus to be 30
			temp_params10.height = 30;
			
			// Set temp_d_plus to be right aligned with temp_d_view
			temp_params10.addRule(RelativeLayout.ALIGN_RIGHT, temp_d_view.getId());
			
			// Set parameters of temp_d_plus
			temp_d_plus.setLayoutParams(temp_params10);
			
			// Set id of temp_d_plus to be 1000+i
			temp_d_plus.setId(1000+i);
			
			// Add temp_d_plus to the layout
			l1.addView(temp_d_plus);
			
			// Add temp_d_plus to d_plus array
			d_plus.add(temp_d_plus);
			
			// Create minus button for this players d
			Button temp_d_minus = new Button(this);

			// Set the text of temp_d_minus to be -
			temp_d_minus.setText("-");
			
			// Define the layout parameters to be used for temp_d_minus
			RelativeLayout.LayoutParams temp_params11 = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		
	 
			// Set temp_d_minus to be below temp_d_view
			temp_params11.addRule(RelativeLayout.BELOW, temp_d_view.getId());
			
			// Set the height of temp_d_minus to be 30
			temp_params11.height = 30;
			
			// Set temp_d_minus to be right and left aligned with temp_d_plus
			temp_params11.addRule(RelativeLayout.ALIGN_RIGHT, temp_d_plus.getId());
			temp_params11.addRule(RelativeLayout.ALIGN_LEFT, temp_d_plus.getId());
			
			// Set parameters of temp_d_minus
			temp_d_minus.setLayoutParams(temp_params11);
			
			// Set id of temp_d_minus to be 1100+i
			temp_d_minus.setId(1100+i);
			
			// Add temp_d_minus to the layout
			l1.addView(temp_d_minus);
			
			// Add temp_d_minus to d_minus array
			d_minus.add(temp_d_minus);

			// Create plus button for this players to
			Button temp_to_plus = new Button(this);

			// Set the text of temp_to_plus to be +
			temp_to_plus.setText("+");
			
			// Define the layout parameters to be used for temp_goals_plus
			RelativeLayout.LayoutParams temp_params12 = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		
	 
			// Set temp_to_plus to be above temp_to_view
			temp_params12.addRule(RelativeLayout.ABOVE, temp_to_view.getId());
			
			// Set the height of temp_to_plus to be 30
			temp_params12.height = 30;
			
			// Set temp_to_plus to be right aligned with temp_to_view
			temp_params12.addRule(RelativeLayout.ALIGN_RIGHT, temp_to_view.getId());
			
			// Set parameters of temp_to_plus
			temp_to_plus.setLayoutParams(temp_params12);
			
			// Set id of temp_to_plus to be 1200+i
			temp_to_plus.setId(1200+i);
			
			// Add temp_to_plus to the layout
			l1.addView(temp_to_plus);
			
			// Add temp_to_plus to to_plus array
			to_plus.add(temp_to_plus);
			
			// Create minus button for this players to
			Button temp_to_minus = new Button(this);

			// Set the text of temp_to_minus to be -
			temp_to_minus.setText("-");
			
			// Define the layout parameters to be used for temp_to_minus
			RelativeLayout.LayoutParams temp_params13 = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		
	 
			// Set temp_to_minus to be below temp_to_view
			temp_params13.addRule(RelativeLayout.BELOW, temp_to_view.getId());
			
			// Set the height of temp_to_minus to be 30
			temp_params13.height = 30;
			
			// Set temp_to_minus to be right and left aligned with temp_to_plus
			temp_params13.addRule(RelativeLayout.ALIGN_RIGHT, temp_to_plus.getId());
			temp_params13.addRule(RelativeLayout.ALIGN_LEFT, temp_to_plus.getId());
			
			// Set parameters of temp_to_minus
			temp_to_minus.setLayoutParams(temp_params13);
			
			// Set id of temp_to_minus to be 1300+i
			temp_to_minus.setId(1300+i);
			
			// Add temp_to_minus to the layout
			l1.addView(temp_to_minus);
			
			// Add temp_to_minus to to_minus array
			to_minus.add(temp_to_minus);
			
			// Save i-1 as the final id of this player
			final int player_id = i-1;
			
			// Set onClickListener for temp_goals_plus
			temp_goals_plus.setOnClickListener(new View.OnClickListener() {
				// Override onClick function
				@Override
				public void onClick(View v) {
					// Increment the goals for this player
					goals.set(player_id, goals.get(player_id)+1);
			
					// Increment team_goals
					team_goals++;
					
					// Change corresponding TextView
					goals_views.get(player_id).setText(String.format("%d", goals.get(player_id)));

					// Team stats views
					updateTeamViews();
				}
			});
			
			// Set onClickListener for temp_goals_minus
			temp_goals_minus.setOnClickListener(new View.OnClickListener() {
				// Override onClick function
				@Override
				public void onClick(View v) {
					// Check if goals for this player are positive
					if(goals.get(player_id) > 0){
						// If so decrement goals for this player
						goals.set(player_id, goals.get(player_id)-1);
						
						// Decrement team_goals
						team_goals--;
					}
					
					// Change corresponding TextView
					goals_views.get(player_id).setText(String.format("%d", goals.get(player_id)));

					// Team stats views
					updateTeamViews();
				}
			});
			
			// Set onClickListener for temp_assists_plus
			temp_assists_plus.setOnClickListener(new View.OnClickListener() {
				// Override onClick function
				@Override
				public void onClick(View v) {
					// Increment the assists for this player
					assists.set(player_id, assists.get(player_id)+1);
					
					// Increment team_assists
					team_assists++;
					
					// Change corresponding TextView
					assists_views.get(player_id).setText(String.format("%d", assists.get(player_id)));

					// Team stats views
					updateTeamViews();
				}
			});
			
			// Set onClickListener for temp_assists_minus
			temp_assists_minus.setOnClickListener(new View.OnClickListener() {
				// Override onClick function
				@Override
				public void onClick(View v) {
					// Check if assists for this player are positive
					if(assists.get(player_id)> 0){
						// If so decrement assists for first player
						assists.set(player_id, assists.get(player_id)-1);
						
						// Decrement team_assists
						team_assists--;
					}
					
					// Change corresponding TextView
					assists_views.get(player_id).setText(String.format("%d", assists.get(player_id)));

					// Team stats views
					updateTeamViews();
				}
			});
			
			// Set onClickListener for temp_d_plus
			temp_d_plus.setOnClickListener(new View.OnClickListener() {
				// Override onClick function
				@Override
				public void onClick(View v) {
					// Increment the d for this player
					d.set(player_id, d.get(player_id)+1);
					
					// Increment team_d
					team_d++;
					
					// Change corresponding TextView
					d_views.get(player_id).setText(String.format("%d", d.get(player_id)));

					// Team stats views
					updateTeamViews();
				}
			});
			
			// Set onClickListener for temp_d_minus
			temp_d_minus.setOnClickListener(new View.OnClickListener() {
				// Override onClick function
				@Override
				public void onClick(View v) {
					// Check if d for this player are positive
					if(d.get(player_id) > 0){
						// If so decrement d for this player
						d.set(player_id, d.get(player_id)-1);
						
						// Decrement team_d
						team_d--;
					}
					
					// Change corresponding TextView
					d_views.get(player_id).setText(String.format("%d", d.get(player_id)));

					// Team stats views
					updateTeamViews();
				}
			});
			
			// Set onClickListener for temp_to_plus
			temp_to_plus.setOnClickListener(new View.OnClickListener() {
				// Override onClick function
				@Override
				public void onClick(View v) {
					// Increment the to for this player
					to.set(player_id, to.get(player_id)+1);
				
					// Increment team_to
					team_to++;
					
					// Change corresponding TextView
					to_views.get(player_id).setText(String.format("%d", to.get(player_id)));

					// Team stats views
					updateTeamViews();
				}
			});
			
			// Set onClickListener for temp_to_minus
			temp_to_minus.setOnClickListener(new View.OnClickListener() {
				// Override onClick function
				@Override
				public void onClick(View v) {
					// Check if to for this player are positive
					if(to.get(player_id) > 0){
						// If so decrement to for first player
						to.set(player_id, to.get(player_id)-1);
						
						// Decrement team_to
						team_to--;
					}
					
					// Change corresponding TextView
					to_views.get(player_id).setText(String.format("%d", to.get(player_id)));

					// Team stats views
					updateTeamViews();
				}
			});
		}
        
        // Define onClick function for save button
		save.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Open the database for reading and writing
				db = db_helper.getWritableDatabase();
				
				// Iterate through all players
				for(int i = 0; i < num_players; i++){
					// Get this player's all time stats from the database
					int[] old_stats = db_helper.getPlayerStats(db, id, i+1);
					
					// Check if this returned -1
					if(old_stats[0] != -1){
						// If not then initialize an array to hold new stats
						int[] new_stats = new int[4];
						
						// Add this games goals to old goals to get new goals
						new_stats[0] = old_stats[0] + goals.get(i);
						
						// Add this games assists to old assists to get new assists
						new_stats[1] = old_stats[1] + assists.get(i);
						
						// Add this games d to old d to get new d
						new_stats[2] = old_stats[2] + d.get(i);
						
						// Add this games to to old to to get new to
						new_stats[3] = old_stats[3] + d.get(i);
						
						// Update this players stats in the database
						db_helper.updateStats(db, id, i+1, new_stats);
					}
				}
				
				// Close the database
				db.close();
				
				// Finish this activity
				finish();
			}
		});
    }
    
    // Override onSavedInstanceState function
    @Override
    public void onSaveInstanceState(Bundle outState){
    	// Call super function
    	super.onSaveInstanceState(outState);
    	
    	// Put id into outState bundle
    	outState.putInt("id", id);
    	
    	// Put num_players into outState bundle
    	outState.putInt("num_players", num_players);
    	
    	// Put goals array into outState bundle
    	outState.putIntegerArrayList("goals", goals);
    	
    	// Put assists array into outState bundle
    	outState.putIntegerArrayList("assists", assists);
    	
    	// Put d array into outState bundle
    	outState.putIntegerArrayList("d", d);
    	
    	// Put to array into outState bundle
    	outState.putIntegerArrayList("to", to);
    	
    	// Put team_goals into outState bundle
    	outState.putInt("team_goals", team_goals);
    	
    	// Put team_assists into outState bundle
    	outState.putInt("team_assists", team_assists);
    	
    	// Put team_d into outState bundle
    	outState.putInt("team_d", team_d);
    	
    	// Put team_to into outState bundle
    	outState.putInt("team_to", team_to);
    }
    
    // Define function update cumulative views
    private void updateTeamViews(){
    	// Initialize 4 TextViews that hold team's stats
    	team_goals_view = (TextView) findViewById(R.id.teamGoals);
    	team_assists_view = (TextView) findViewById(R.id.teamAssists);
    	team_d_view = (TextView) findViewById(R.id.teamD);
    	team_to_view = (TextView) findViewById(R.id.teamTo);
    	
    	// Update text in each view based off corresponding stats
    	team_goals_view.setText(String.format("%d", team_goals));
    	team_assists_view.setText(String.format("%d", team_assists));
    	team_d_view.setText(String.format("%d", team_d));
    	team_to_view.setText(String.format("%d", team_to));
    }
}
